<?
  include ("../../../admin/config/cfg.php");
  global $cwlang,$cwUserInfo,$cwUserId;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  //================ XAJAX ================
  function sendDial($number,$ip,$http_username,$http_password,$http_scheme){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Process request
    $params=array(
    		array("key"=>"NUMBER","value"=>$number)
      );
    $asterisk->phoneAction("dial",$ip,$params,$http_username,$http_password,$http_scheme);
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  function sendHangup($ip,$http_username,$http_password,$http_scheme){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Process request
    $asterisk->phoneAction("hangup",$ip,array(),$http_username,$http_password,$http_scheme);
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("sendDial");
  $xajax->registerFunction("sendHangup");
  $xajax->processRequests();
  
  //================ PAGE LOGIC ================
  if(!isset($_GET["contact"])||($_GET["contact"]=="")) die("Error: missing contact reference");
  //Set client ip
  $ip=$_GET["ip"];
  $http_username=$_GET["http_username"];
  $http_password=$_GET["http_password"];
  $http_scheme=$_GET["http_scheme"];
  //Get the contact informations
  $ret=$asterisk->getContact($_GET["contact"],0);
  if(!is_array($ret)) die;
  $c=$ret[0];
  $id=$c["id"];
  $id_user=$c["id_user"];
  $read=$c["read"];
  $write=$c["write"];
  $name=$c["name"];
  $surname=$c["surname"];
  $company=$c["company"];
  $phonenumber=$c["phonenumber"];
  $mobile=$c["mobile"];
  //logic
  if(($read!=1)&&($cwUserId!=$id_user)&&($cwUserInfo["type"]==USER_STANDARD)) die();
  //html
  if(trim($phonenumber)!=""){ 
    $html.="<div id=\"dial-fixed\" style=\"background: url($relBasePath/public/img/dial-fixed.jpg) no-repeat;\" onclick=\"xajax_sendDial('".$phonenumber."','".$ip."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\">".$phonenumber."</div>";
  }else $html.="<div><img src=\"$relBasePath/public/img/dial-fixed-dis.jpg\"/></div>";
  if(trim($mobile)!=""){ 
    $html.="<div id=\"dial-mobile\" style=\"background: url($relBasePath/public/img/dial-mobile.jpg) no-repeat;\" onclick=\"xajax_sendDial('".$mobile."','".$ip."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\">".$mobile."</div>";
  }else $html.="<div><img src=\"$relBasePath/public/img/dial-mobile-dis.jpg\"/></div>"; 
  $html.="<div><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/dial-hangup.jpg\" onclick=\"xajax_sendHangup('".$ip."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\" /><div>";
  
  //================ MAIN CONTENTS ================
  $xajax->printJavascript("../../admin/lib/xajax/");
?>
  <div style="text-align: center;">
    <h1 style="color: #333; font-size: 24px; line-height: 1.5em; padding: 0; margin: 0;"><?=$name?> <?=$surname?></h1>
    <h3 style="padding: 0; margin: 0;"> <?=$company?> </h3>
    <?=$html?>
  </div>